package pt.ua.helpdesk.client;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * This class extends a composite and will allow the
 * user to activate an account. It must contain a space
 * to introduce the activation code and the email
 * 
 * @author Amigos do Noddy
 */
public class LoginActivar extends Composite {

	/**
	 * Constructor. 
	 */
	public LoginActivar() {

		final HorizontalPanel horizontalPanel = new HorizontalPanel();
		initWidget(horizontalPanel);
		horizontalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		final VerticalPanel verticalPanel = new VerticalPanel();
		horizontalPanel.add(verticalPanel);
		verticalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);

		final HorizontalPanel horizontalPanel_1 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_1);
		horizontalPanel_1.setSpacing(8);

		final Image image = new Image();
		horizontalPanel_1.add(image);
		image.setUrl("images/logo.png");

		final HorizontalPanel horizontalPanel_6 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_6);
		horizontalPanel_6.setSpacing(2);

		final Label emailLabel = new Label("E-mail:");
		horizontalPanel_6.add(emailLabel);
		emailLabel.setStyleName("label");

		final TextBox txtEmail = new TextBox();
		horizontalPanel_6.add(txtEmail);
		txtEmail.setWidth("250");
		txtEmail.setStyleName("textbox");

		final HorizontalPanel horizontalPanel_2 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_2);

		final Label label = new Label("Código de Activação:");
		horizontalPanel_2.add(label);
		label.setStyleName("label");

		final TextBox txtCodigo = new TextBox();
		horizontalPanel_2.add(txtCodigo);
		txtCodigo.setWidth("250");
		txtCodigo.setStyleName("textbox");

		final HorizontalPanel horizontalPanel_7 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_7);
		horizontalPanel_7.setSpacing(4);

		final Button btnVoltar = new Button();
		horizontalPanel_7.add(btnVoltar);
		btnVoltar.addClickListener(new ClickListener() {
			public void onClick(final Widget sender) {
				Index.voltar();
			}
		});
		btnVoltar.setStyleName("buttonVoltar");
		btnVoltar.setText("« Voltar");

		final HTML htmlAlerta = new HTML("");
		
		final Button btnActivar = new Button();
		horizontalPanel_7.add(btnActivar);
		btnActivar.addClickListener(new ClickListener() {
			public void onClick(final Widget sender) {
				boolean alerta = false;
				int c = 0;
							
				if(txtEmail.getText().equals(""))
				{
					if(c == 0)
					{
						htmlAlerta.setHTML("<u>ALERTA:</u><br>");
						c++;
					}
					alerta = true;
					htmlAlerta.setHTML(htmlAlerta.getHTML() + "» tem de introduzir o E-mail! <br>");
				}
				else
					if(!txtEmail.getText().matches(".+@.+\\.[a-z]+"))
					{
						if(c == 0)
						{
							htmlAlerta.setHTML("<u>ALERTA:</u><br>");
							c++;
						}
						alerta = true;
						htmlAlerta.setHTML(htmlAlerta.getHTML() + "» o e-mail não é válido! <br>");
					}
				
				if(txtCodigo.getText().equals(""))
				{
					if(c == 0)
					{
						htmlAlerta.setHTML("<u>ALERTA:</u><br>");
						c++;
					}
					alerta = true;
					htmlAlerta.setHTML(htmlAlerta.getHTML() + "» tem de introduzir o código de activação! <br>");
				}
				
				if(!alerta)
				{
					htmlAlerta.setHTML("a processar dados...");
					ServiceLogin.Util.getInstance().activar(txtEmail.getText(), Long.parseLong(txtCodigo.getText().trim()), new ActivarContaAsyncCallback(htmlAlerta));
				}

				
			}
		});
		btnActivar.setStyleName("button");
		btnActivar.setText("Activar");

		final HorizontalPanel horizontalPanel_8 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_8);
		horizontalPanel_8.setSpacing(2);


		horizontalPanel_8.add(htmlAlerta);
		htmlAlerta.setStyleName("erro");
	}
	
	/**
	 * An asynchronous callback is the primary interface that a caller
	 * must implement to receive a response from a remote procedure call<br>
	 * <br>
	 * This class is the implementation of AsyncCallback for the activate
	 * account service
	 * 
	 * @author Amigos do Noddy
	 */
	static class ActivarContaAsyncCallback implements AsyncCallback
	{
		HTML html;
		
		/**
		 * Constructor
		 * 
		 * @param html Widget that can contain arbitrary HTML
		 */
		public ActivarContaAsyncCallback(HTML html)
		{
			this.html= html;
		}
		
		/**
		 * Success handler
		 */
		public void onSuccess(Object object)
		{
			Boolean ret = (Boolean)object;
			String message = "";
			
			if(ret.booleanValue())
				message = "Activou a sua conta com sucesso! <br> já pode fazer login no sistema.";
			else
				message = "Código de activação incorrecto!<br> contacte o Administrador.";
			  

			DOM.setInnerHTML(html.getElement(), message);
		}
		  
		/**
		 * Failure handler
		 */
		public void onFailure(Throwable throwable)
		{
			String message = "ocorreu um erro inesperado!";
			DOM.setInnerHTML(html.getElement(), message);
		}
	}
}
